package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.RoleService;
import com.cyz.admin.service.Admin_UserRoleService;
import com.cyz.common.bean.Role;
import com.cyz.common.bean.UserRole;
import com.cyz.common.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 34928
* @description 针对表【user_role(用户和角色关联表)】的数据库操作Service实现
* @createDate 2023-02-24 08:58:49
*/
@Service
public class AdminUserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole>
    implements Admin_UserRoleService {

    @Autowired
    private RoleService roleService;

    @Override
    public List<Role> getRolesByUserId(Integer id) {
        //创建筛选器
        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据用户id查询角色id
        lambdaQueryWrapper.eq(UserRole::getUserId,id);
        //查询数据
        List<UserRole> list = list(lambdaQueryWrapper);
        List<Role> roles = new ArrayList<>();
        //转化数据
        list.stream()
                .map(UserRole::getRoleId)
                .forEach(item -> roles.add(roleService.getById(item)));

        return roles;
    }
}




